package com.etc.zmall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etc.dao.ShopcMapper;
import com.etc.entity.Shopc;
import com.etc.entity.ShopcExample;
import com.etc.zmall.service.ShopCartService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 
 * <p>
 * Title: ShopCartServiceImpl
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * @author knowno
 * 
 * @date 2021年1月11日
 * 
 */
@Service
public class ShopCartServiceImpl implements ShopCartService {

	@Autowired
	private ShopcMapper shopcMapper;

	/**
	 * (non-Javadoc)
	
	* <p>Title: showShopCar</p>
	
	* <p>Description: </p>
	
	* @param userid
	* @param page
	* @param limit
	* @param keywords
	* @return
	
	* @see com.etc.zmall.service.ShopCartService#showShopCar(int, int, int, java.lang.String)
	 */
	@Override
	public PageInfo<Shopc> showShopCar(int userid, int page, int limit, String keywords) {
		if (page < 1) {
			page = 1;
		}
		ShopcExample example = new ShopcExample();
		example.createCriteria().andGoodsnameLike("%" + keywords + "%").andUseridEqualTo(userid);
		// 分页查询
		PageHelper.startPage(page, limit);
		List<Shopc> list = shopcMapper.selectByExample(example);
		// 返回一个pageInfo对象包含了分页有关的各种属性，相当于我们以前的pageData
		PageInfo<Shopc> pd = new PageInfo<Shopc>(list);
		return pd;
	}

	@Override
	public boolean delGoods(int userid, String goodsname) {
		ShopcExample example = new ShopcExample();
		example.createCriteria().andUseridEqualTo(userid).andGoodsnameEqualTo(goodsname);
		boolean flag = shopcMapper.deleteByExample(example) > 0;
		if (flag)
			return true;
		return false;
	}

	@Override
	public boolean addGoods(Shopc shopc) {
		boolean flag = false;
		// 业务逻辑判断
		// 1 判断这个商品 是否存在，根据 goodsi usersid去查询
		Shopc shopcOld = shopcMapper.selectByGoodsIdAndUserId(shopc.getUserid(), shopc.getGoodsid());

		// 2如果不存在
		if (shopcOld == null) {
			// 2-1增加操作
			flag = shopcMapper.insert(shopc) > 0;
		} else {
			// 2-2 存在该商品记录
			// 需要将shopcOld中的shopcid拿过来,作为修改的条件
			shopc.setShopcid(shopcOld.getShopcid());
			// 因为之前传递过的是price
			flag = shopcMapper.updateByPrimaryKeySelective(shopc) > 0;
		}
		return flag;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * <p>
	 * Title: selectByGoodsIdAndUserId
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param userid
	 * @param goodsid
	 * @return
	 * 
	 * @see com.etc.zmall.service.ShopCartService#selectByGoodsIdAndUserId(java.lang.Integer,
	 *      java.lang.Integer)
	 * 
	 */
	@Override
	public Shopc selectByGoodsIdAndUserId(Integer userid, Integer goodsid) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * <p>
	 * Title: showShopName
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param shopc
	 * @return
	 * 
	 * @see com.etc.zmall.service.ShopCartService#showShopName(com.etc.entity.Shopc)
	 * 
	 */
	@Override
	public Shopc showShopName(Shopc shopc) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * <p>
	 * Title: updateShopCart
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param shopc
	 * @return
	 * 
	 * @see com.etc.zmall.service.ShopCartService#updateShopCart(com.etc.entity.Shopc)
	 * 
	 */
	@Override
	public boolean updateShopCart(Shopc shopc) {
		// TODO Auto-generated method stub
		return false;
	}

}
